//
//  AddMenuViewController.m
//  Spa
//
//  Created by PangPond on 12/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "AddMenuViewController.h"
#import "Menu.h"
@interface AddMenuViewController ()

@end

@implementation AddMenuViewController

@synthesize addMenuPriceCell = _addMenuPriceCell;
@synthesize addMenuNameCell = _addMenuNameCell;
@synthesize databaseUtility = _databaseUtility;
@synthesize fieldName,fieldPrice;
@synthesize keyMenuName = _keyMenuName;
@synthesize keyIf;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //[self.tableView reloadData];
    [self loadView];
    [self viewDidLoad];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *menu = [self.databaseUtility fetchedResultsWithClass:@"Menu" withSortDescriptorString:@"name" withPredicateString:[NSString stringWithFormat:@"name == '%@'",self.keyMenuName]];
    
    Menu *menus = [menu lastObject];
    
    if (indexPath.row == 0) {
        static NSString *CellIdentifier = @"AddMenuNameCell";
        
        AddMenuNameCell *cell = (AddMenuNameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddMenuNameCell" owner:self options:nil];
            
            cell = self.addMenuNameCell;
            self.addMenuNameCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = menus.name;
        
        return cell;
    }else {
        static NSString *CellIdentifier = @"AddMenuPriceCell";
        
        AddMenuPriceCell *cell = (AddMenuPriceCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddMenuPriceCell" owner:self options:nil];
            
            cell = self.addMenuPriceCell;
            self.addMenuPriceCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = menus.price;
        
        return cell;
    }
    
}


-(IBAction)BackButton:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//validation

-(BOOL)validate: (NSString *) candidate{
    NSString *menuName = @"[A-Z0-9a-zก-ฮ๑-๙.]{1,50}";
    
    //@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *menuNames = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", menuName];
    
    
    return [menuNames evaluateWithObject:candidate];
}
-(BOOL)validateInt: (NSString *) candidate{
    NSString *productPrice = @"[0-9]{1,50}";
    NSPredicate *productPrices = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", productPrice];
    return [productPrices evaluateWithObject:candidate];
}
-(IBAction)SaveButton:(id)sender{
    
    if([self validate:[fieldName text]] ==1)
	{
        
        if([self validateInt:[fieldPrice text]] ==1){
            
            
            if (keyIf == @"1") {
                
                NSArray *menus = [self.databaseUtility fetchedResultsWithClass:@"Menu" withSortDescriptorString:@"name" withPredicateString:[NSString stringWithFormat:@"name == '%@'",self.keyMenuName]];
                
                Menu *menu = [menus lastObject];
                menu.name =fieldName.text;
                menu.price = fieldPrice.text;
                
                [self.databaseUtility saveContext];
                [self dismissViewControllerAnimated:YES completion:nil];
            }else{
                Menu *menu = [NSEntityDescription insertNewObjectForEntityForName:@"Menu" inManagedObjectContext:self.databaseUtility.managedObjectContext];
                menu.name =fieldName.text;
                menu.price = fieldPrice.text;

                [self.databaseUtility saveContext];
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"NO1" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
        }
        
        
	}else{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"NO" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
		[alert show];
	}
    
}
@end
